package com.jh.paymentchannel.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.jh.paymentchannel.pojo.SFAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jh.paymentchannel.business.SFAuthBusiness;

import cn.jh.common.utils.CommonConstants;

@Controller
@EnableAutoConfiguration
public class SFAuthService {

	private static final Logger log = LoggerFactory.getLogger(SFAuthService.class);

	@Autowired
	private SFAuthBusiness sfAuthBusiness;

	/**
	 * 增加SFAuth验证信息
	 * 
	 * @param request
	 * @param name
	 * @param phone
	 * @param accNo
	 * @param accBankName
	 * @param accBankNo
	 * @param bankPhone
	 * @param card
	 * @param mrtNo
	 * @param settleRate
	 * @param extraRateType
	 * @param extraTate
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/sfauth/add")
	public @ResponseBody Object addSFAuth(HttpServletRequest request, 
			@RequestParam(value = "name") String name,
			@RequestParam(value = "phone") String phone, 
			@RequestParam(value = "accNo") String accNo,
			@RequestParam(value = "accBankName") String accBankName,
			@RequestParam(value = "accBankNo") String accBankNo, 
			@RequestParam(value = "bankPhone") String bankPhone,
			@RequestParam(value = "card") String card, 
			@RequestParam(value = "mrtNo") String mrtNo,
			@RequestParam(value = "settleRate") String settleRate, 
			@RequestParam(value = "secretKey") String secretKey,
			@RequestParam(value = "dataKey") String dataKey,
			@RequestParam(value = "extraRateType", required = false) String extraRateType,
			@RequestParam(value = "extraRate", required = false) String extraRate,
			@RequestParam(value = "typeMp", required = false) String typeMp,
			@RequestParam(value = "typeEpays", required = false) String typeEpays,
			@ModelAttribute SFAuth model
			) {
		model.setCreateTime(new Date());
		Map map = new HashMap();
		SFAuth sfAuth;
		try {
			sfAuth = sfAuthBusiness.findSFAuthByPhone(model.getPhone());
			if (sfAuth != null) {
				map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
				map.put(CommonConstants.RESP_MESSAGE, "添加失败,表中已有该手机号记录");
			} else {
				map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
				map.put(CommonConstants.RESULT, sfAuthBusiness.save(model));
				map.put(CommonConstants.RESP_MESSAGE, "添加成功");
			}
		} catch (Exception e) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "参数有误,操作数据库异常");
		}

		return map;
	}

	/**
	 * 根据手机号查询SFAuth
	 * 
	 * @param request
	 * @param phone
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/sfauth/findbyphone")
	public @ResponseBody Object findSFAuthByPhone(HttpServletRequest request,
			@RequestParam(value = "phone") String phone) {

		Map map = new HashMap();
		SFAuth sfAuth = sfAuthBusiness.findSFAuthByPhone(phone);

		if (sfAuth == null) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "根据手机号查询不到该条数据");
			return map;
		}

		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "查询成功");
		map.put(CommonConstants.RESULT, sfAuth);
		return map;
	}

	/**
	 * 根据手机号修改SFAuth的信息
	 * 
	 * @param request
	 * @param name
	 * @param phone
	 *            必须
	 * @param accNo
	 * @param accBankName
	 * @param accBankNo
	 * @param bankPhone
	 * @param card
	 * @param mrtNo
	 * @param settleRate
	 * @param extraRateType
	 * @param extraRate
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/sfauth/update")
	public @ResponseBody Object updateSFAuth(HttpServletRequest request,
			@RequestParam(value = "name", required = false) String name, @RequestParam(value = "phone") String phone,
			@RequestParam(value = "accNo", required = false) String accNo,
			@RequestParam(value = "accBankName", required = false) String accBankName,
			@RequestParam(value = "accBankNo", required = false) String accBankNo,
			@RequestParam(value = "bankPhone", required = false) String bankPhone,
			@RequestParam(value = "card", required = false) String card,
			@RequestParam(value = "mrtNo", required = false) String mrtNo,
			@RequestParam(value = "secretKey", required = false) String secretKey,
			@RequestParam(value = "dataKey", required = false) String dataKey,
			@RequestParam(value = "settleRate", required = false) String settleRate,
			@RequestParam(value = "typeMp", required = false) String typeMp,
			@RequestParam(value = "typeEpays", required = false) String typeEpays,
			@RequestParam(value = "extraRateType", required = false) String extraRateType,
			@RequestParam(value = "extraRate", required = false) String extraRate) {
		Map map = new HashMap();
		SFAuth sfAuth = sfAuthBusiness.findSFAuthByPhone(phone);
		if (sfAuth == null) {
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "根据手机号查询不到该条数据");
			return map;
		} else {
			if (name != null) {
				sfAuth.setName(name);
			}
			if (accNo != null) {
				sfAuth.setAccNo(accNo);
			}
			if (accBankName != null) {
				sfAuth.setAccBankName(accBankName);
			}
			if (accBankNo != null) {
				sfAuth.setAccBankNo(accBankNo);
			}
			if (bankPhone != null) {
				sfAuth.setBankPhone(bankPhone);
			}
			if (card != null) {
				sfAuth.setCard(card);
			}
			if (mrtNo != null) {
				sfAuth.setMrtNo(mrtNo);
			}
			if (secretKey != null) {
				sfAuth.setSecretKey(secretKey);
			}
			if (dataKey != null){
				sfAuth.setDataKey(dataKey);
			}
			if (settleRate != null) {
				sfAuth.setSettleRate(settleRate);
			}
			if (extraRateType != null) {
				sfAuth.setExtraRateType(extraRateType);
			}
			if (extraRate != null) {
				sfAuth.setExtraRate(extraRate);
			}
			if (typeMp != null) {
				sfAuth.setTypeMp(typeMp);
			}
			if (typeEpays != null) {
				sfAuth.setTypeEpays(typeEpays);
			}
			try {
				sfAuth = sfAuthBusiness.updateSFAuth(sfAuth);
			} catch (RuntimeException e) {
				map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
				map.put(CommonConstants.RESP_MESSAGE, "操作数据库异常");
				return map;
			}
		}

		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESP_MESSAGE, "根据手机号修改成功");
		return map;
	}

}
